<template>
    <view class="content">
        <view class="address">
            <view class="edit">
                <view class="u-flex u-row-between u-col-top">
                    <view class="edit-title">服务地址</view>
                    <view class="select">
                        <pick-regions @getRegion="handleGetRegion">
                            <view class="adr-box">
                                <view v-if="region.length > 0">{{ region[0].name }}{{ region[1].name }}{{ region[2].name }}</view>
                                <view v-else class="place">请选择省市区</view>
                            </view>
                        </pick-regions>
                    </view>
                </view>
                <view class="bor-top u-flex u-row-between u-col-top">
                    <view class="edit-title"></view>
                    <input type="text" v-model="address" placeholder="详细楼号 / 楼层 / 门牌号" placeholder-style="color:#9b9b9b">
                </view>
                
                <view class="bor-top u-flex u-row-between">
                    <view class="edit-title">紧急联系人</view>
                    <input type="text" v-model="name" placeholder="请填写紧急联系人姓名" placeholder-style="color:#9b9b9b">
                </view>
                <view class="bor-top u-flex u-row-between">
                    <view class="edit-title">紧急联系电话</view>
                    <input type="number" v-model="tel" placeholder="请输入11位手机号码" placeholder-style="color:#9b9b9b">
                </view>
                
                <view class="frame u-flex u-row-between bor-top">
                	<view class="frame-tit">设置默认地址</view>
                	<u-switch v-model="checked" size="40" active-color="#f29700" @change="change"></u-switch>
                </view>
                <image :src="IMAGEURL + 'static/icon/line.png'" class="line"></image>
            </view>
        </view>
        <view class="address-btn" @click="doSubmit">确认</view>
    </view>
</template>

<script>
    import { IMAGEURL } from '@/static/js/common.js';
    import pickRegions from '@/components/pick-regions/pick-regions.vue';
	export default {
        components: {
            pickRegions
        },
		data() {
			return {
                IMAGEURL,
				checked: true,
                name: '',
                tel: '',
                region: [], //省市区
                address: '', //详细地址
                
                provinceVal: '',
                cityVal: '',
                areaVal: '',
                
                item: '',
                is_default: '',
                
                id: '',
			}
		},
        onLoad(option) {
            if(option.id){
            	uni.setNavigationBarTitle({
            		title:'修改地址'
            	})
                this.id = option.id
            	this.getData()
            }
        },
		methods: {
            //选择地区
            handleGetRegion(region) {
                console.log(region);
                this.region = region;
            },
            
			change(status) {
				console.log(status);
                this.checked = status;
			},
            doSubmit(){
                if (this.region.length <= 0) {
                    this.$toast('请选择地址');
                    return false;
                }
                if(!this.address){
                	this.$toast('请输入详细地址')
                	return
                }
                if(!this.name){
                	this.$toast('请输入紧急联系人姓名')
                	return
                }
                if(!this.tel){
                	this.$toast('请输入紧急联系人手机号')
                	return
                }
                if(!this.$checkPhone(this.tel)){
                	this.$toast('请输入正确的手机号')
                	return
                }
            	this.$ajax('insert_address', {
                    user_token: this.$getSync('userToken'),
            		name: this.name,
            		mobile: this.tel,
                    province: this.region[0].name,
                    city: this.region[1].name,
                    area: this.region[2].name,
                    address: this.address,//地址
                    id: this.id ? this.id : '', //地址id 编辑传
                    is_default: this.checked ? 1 : 0, //1默认 0不默认
            	}).then(ret => {
            		if (ret.success == 1000) {
                        this.$toast('添加成功！');
            			uni.navigateBack({
            				delta:1
            			})
            		} else {
            			this.$toast(ret.msg);
            		}
            	});
            },
            
            getData(){
            	this.$ajax('select_address', {
            		user_token: this.$getSync('userToken'),
                    id: this.id,
            	}).then(ret => {
            		if (ret.success == 1000) {
                        let data = ret.detail[0]
            			this.address = data.address
            			this.region = [
            			    {
            			        name: data.province
            			    },
            			    {
            			        name: data.city
            			    },
            			    {
            			        name: data.area
            			    }
            			];
                        this.name = data.name
                        this.tel = data.mobile
            			this.is_default = data.is_default
                        this.checked = data.is_default ? true : false
            		} else {
            			this.$toast(ret.msg);
            		}
            	});
            },
		}
	}
</script>

<style>
    page {
    	background-color: #f5f5f5;
    }
</style>

<style lang="scss" scoped>
    .select {
        flex: 1;
        display: flex;
        align-items: center;
        height: 100upx;
    }
    .adr-box {
        // width: 76vw;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .adr-box > view {
        width: 100%;
        font-size: 28upx;
    }
    .place {
        font-size: 28upx;
        color: #9b9b9b;
        margin-right: 10upx;
    }
    

	.frame {
		width: 100%;
		background-color: #fff;
		padding: 0 20rpx;
		border-bottom: 1rpx solid #e6e6e6;

		.content:last-child .frame {
			border-bottom: 0;
		}

		.frame-tit {
			min-width: 160rpx;
			font-size: 28rpx;
			color: #313131;
			line-height: 110rpx;
			text-align: left;
		}



		.frame-con {
			font-size: 30rpx;
			color: #0c0c0c;
			line-height: 110rpx;
		}

		input {
			flex: 1;
			height: 110rpx;
			font-size: 30rpx;

			.p1 {
				color: #b9b9b9;
			}
		}

		.i1::after {
			content: '';
			width: 632rpx;
			height: 1rpx;
			background-color: #e6e6e6;
			position: absolute;
			top: 0;
			right: -20rpx;
		}

		.z-right {
			width: 16rpx;
			height: 29rpx;
		}

	}

	.line {
		width: 746rpx;
		height: 2rpx;

	}

	.btn {
		width: 686rpx;
		height: 92rpx;
		font-size: 34rpx;
		color: #fff;
		line-height: 92rpx;
		text-align: center;
		border-radius: 46rpx;
		background: #b00b0e;
		margin: 150rpx auto 0;

	}
    
    
    .content {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        width: 100%;
        // padding: 20rpx;
    }
    
    // 地址列表
    .address {
        width: 100%;
    
        .edit {
            width: 100%;
            background-color: #fff;
    
            .edit-title {
                width: 210rpx;
                font-size: 28rpx;
                color: #313131;
                line-height: 110rpx;
                padding-left: 20rpx;
                // padding-right: 20rpx;
            }
    
            input {
                flex: 1;
                width: 100%;
                height: 110rpx;
                font-size: 30rpx;
                color: #313131;
                padding-right: 20rpx;
            }
    
            .sex {
                width: 130rpx;
                font-size: 30rpx;
                color: #7b7b7b;
                line-height: 58rpx;
                text-align: center;
                border-radius: 10rpx;
                border: 1rpx solid #e6e6e6;
                position: relative;
                overflow: hidden;
    
                image {
                    display: none;
                    width: 31rpx;
                    height: 27rpx;
                    position: absolute;
                    top: 0;
                    right: 0;
                }
            }
    
            .active {
                color: #f99f24;
                border: 1rpx solid #f99f24;
    
                image {
                    display: block;
    
                }
            }
    
        }
    
        .bor-top {
            border-top: 1rpx solid #e6e6e6;
        }
    
        .icon {
            display: block;
            width: 25rpx;
            height: 32rpx;
        }
    
        .line {
            display: block;
            width: 750rpx;
            height: 3rpx;
        }
    }
    
    
    
    // 按钮
    .address-btn {
        width: 686rpx;
        font-size: 34rpx;
        color: #fff;
        line-height: 94rpx;
        text-align: center;
        border-radius: 15rpx;
        background: #f29700;
        margin: 70rpx auto;
    }
</style>
